package com.ax.api.route.base.common;

import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.ax.api.route.util.RouteConfig;

@RestController
@RequestMapping("/org")
public class OrgRoute {

	 @Autowired
	 LoadBalancerClient loadBalancerClient;
	 @Autowired
	 RestTemplate restTemplate;
	 
	/**
	 * 菜单页面的跳转
	 */
	@GetMapping("/sys/org")
	String drugAdministration(Model model) {
		String url = geturl("/sys/org",RouteConfig.BASE_ROUTE);
		return restTemplate.getForObject(url, String.class);
	}
	
	/**
	 * add
	 * remote_base
	 * 组织机构(模块:组织机构)*跳转增加页面
	 */   
	@GetMapping("/add/org")
	public String addDrugAdministration(String pId){
		String value = "/pId="+pId;
		String url = geturl("/sys/org/add",RouteConfig.BASE_ROUTE)+value;
		return restTemplate.getForObject(url, String.class,pId);
  }
	
	/**
	 * edit
	 * remote_base
	 * 组织机构(模块:组织机构)*跳转修改页面
	 */   
	@GetMapping("/edit/org")
	public String edit(String id){
		String value = "/id="+id;
		String url = geturl("/sys/org/edit",RouteConfig.BASE_ROUTE)+value;
		return restTemplate.getForObject(url, String.class,id);
  }
	
	/**
	 * queryBaseOrg
	 * remote_bussiness
	 * 组织机构查询(模块:组织机构)
	 */ 
	@GetMapping("/queryBaseOrg")
	public String insertDrugDep(@RequestParam Map<String,Object> map){
		String value = "?orgId={orgId}";
		String url = geturl("/queryBaseOrg",RouteConfig.BUSSINESS_ROUTE)+value;
		return restTemplate.getForObject(url, String.class,map);
  }
	
	/**
	 * insertBaseOrg
	 * remote_bussiness
	 * 组织机构新增(模块:组织机构)
	 */ 
	@GetMapping("/insertBaseOrg")
	public String insertBaseOrg(@RequestParam Map<String,Object> map){
		String value = "?orgId={orgId}&orgName={orgName}&orgShortName={orgShortName}&firstLetter={firstLetter}&areaCode={areaCode}&areaName={areaName}&gradeCode={gradeCode}&gradeName={gradeName}&introduction={introduction}&phone={phone}&address={address}&longitude={longitude}&latitude={latitude}&remarks={remarks}&sortNumber={sortNumber}&parentOrgId={parentOrgId}&orgTypeCode={orgTypeCode}&orgTypeName={orgTypeName}&flag={flag}";
		String url = geturl("/insertBaseOrg",RouteConfig.BUSSINESS_ROUTE)+value;
		return restTemplate.getForObject(url, String.class,map);
  }
	
	/**
	 * updateBaseOrg
	 * remote_bussiness
	 * 组织机构修改(模块:组织机构)
	 */  
	@GetMapping("/updateBaseOrg")
	public String updateDrugDep(@RequestParam Map<String,Object> map){
		String value = "?orgId={orgId}&orgName={orgName}&orgShortName={orgShortName}&firstLetter={firstLetter}&areaCode={areaCode}&areaName={areaName}&gradeCode={gradeCode}&gradeName={gradeName}&introduction={introduction}&phone={phone}&address={address}&longitude={longitude}&latitude={latitude}&remarks={remarks}&sortNumber={sortNumber}&parentOrgId={parentOrgId}&orgTypeCode={orgTypeCode}&orgTypeName={orgTypeName}&flag={flag}";
		String url = geturl("/updateBaseOrg",RouteConfig.BUSSINESS_ROUTE)+value;
		return restTemplate.getForObject(url,String.class,map);
  }
	
	private String geturl(String path,String route){
		 ServiceInstance serviceInstance = loadBalancerClient.choose(route);
		 StringBuffer bf = new StringBuffer("http://");
		 bf.append(serviceInstance.getHost());
		 bf.append(":");
		 bf.append(serviceInstance.getPort());
		 bf.append(path);
		 return bf.toString();
	   }
}
